<template>
  <div>
    <el-dialog
      title="资源下载"
      :visible.sync="dialogVisible"
      width="1000px"
      :lock-scroll="false"
    >
      <div class="download-list" style="min-height: 400px">
        <el-table
          :data="fileList"
          stripe
          style="width: 100%"
          height="400"
          size="mini"
        >
          <el-table-column
            prop="name"
            label="资源名称"
            show-overflow-tooltip=""
          >
            <template slot-scope="scope">
              {{ scope.row.sysAttachment.oldName || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="size" label="资源大小" width="160">
            <template slot-scope="scope">
              {{ scope.row.sysAttachment.attrSize | fileSizeFilter }}
            </template>
          </el-table-column>
          <el-table-column prop="type" label="资源类型" width="160">
            <template slot-scope="scope">
              {{ scope.row.sysAttachment.attrSuffixStr || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="160">
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="{ row }">
              <el-button-group>
                <el-button type="primary" size="mini" @click="handleQuote(row)">
                  引用查重
                </el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="quote-wrap">
        <el-form :model="form" label-width="120px">
          <el-form-item label="选择引用班级：">
            <p class="txt-explain" v-if="!classroom.length">
              该功能需要配置教学任务表，请在用户个人中心添加教学班次信息
            </p>
            <el-checkbox-group
              v-model="form.classroom"
              @change="handleChange"
              v-else
            >
              <el-checkbox v-for="v in classroom" :key="v.id" :label="v.id">
                {{ v.gradeName + v.className }}</el-checkbox
              >
            </el-checkbox-group>
            <el-radio label="1" v-model="form.quoteType" @change="handleChange"
              >不引用仅下载</el-radio
            >
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          :disabled="form.quoteType != 1 && !form.classroom.length"
          @click="handleDownload()"
          >下 载</el-button
        >
        <!-- <el-button type="primary" @click="handleQuoteAdd()">添加引用</el-button> -->
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <QuoteList />
  </div>
</template>
<script>
import { eduTeachingQuery } from '@/api/admin/teach/eduTeaching.js'
import { eduResourceQuoteAdd } from '@/api/admin/teach/eduResourceQuote.js'
import Bus from '@/utils/bus.js'
import download from '@/mixins/download'
export default {
  mixins: [download],
  components: {
    QuoteList: () => import('./quoteList.vue'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      form: {
        quoteType: '1',
        classroom: [],
      },
      listQuery: {
        page: 1,
        limit: 8,
        type: 3,
      },
      classroom: [],
      fileList: [],
    }
  },
  mounted() {
    Bus.$on('showDialogDownload', (v) => {
      let { level } = this.$store.getters
      if (level != 1) {
        this.warnMsg('抱歉，您没有下载权限')
        return
      }
      if (!v.length) {
        this.$message({
          message: '请先选择文件',
          type: 'warning',
        })
        return
      }
      this.fileList = v
      this.getClass()
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogDownload')
  },
  methods: {
    getClass() {
      eduTeachingQuery({ teacherCode: this.$store.getters.account }).then(
        (res) => {
          this.classroom = res.data.data
          this.dialogVisible = true
        },
      )
    },
    handleChange(v) {
      if (v == 1) {
        this.form.classroom = []
      } else if (this.form.classroom.length) {
        this.form.quoteType = ''
      }
    },
    //添加引用
    handleQuoteAdd() {
      // if (!this.form.classroom.length) return;
      if (!this.fileList.length) return
      let data = {
        fileId: this.fileList.map((v) => v.fileId).toString(), //文件集合
        eduTeachingIds: this.form.classroom.toString() || '', //班次集合
      }
      eduResourceQuoteAdd(data).then((res) => {})
    },
    //查看引用
    handleQuote(v) {
      Bus.$emit('showDialogQuote', v)
    },
    //下载
    handleDownload() {
      this.handleQuoteAdd()
      this.download(this.fileList)
      this.dialogVisible = false
    },
  },
}
</script>
<style scoped>
.quote-wrap {
  padding-top: 20px;
  height: 100px;
}
.quote-wrap >>> .el-radio__input.is-checked .el-radio__inner {
  background: #930b0b !important;
  border-color: #930b0b !important;
}
.quote-wrap >>> .el-radio__input.is-checked + .el-radio__label {
  color: #930b0b !important;
}
</style>